<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片试图区域 -->
    <el-card>
      <!-- 顶部区域 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click="addCate">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 主体表格树区域 -->
      <tree-table :data="cateList" :columns='columns' :selection-type='false' :expand-type='false' show-index index-text border stripe>
        <template slot="isOk" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted == false" style="color: #73d9e8"></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        <template slot="sort" slot-scope="scope">
          <el-tag type="success" v-if="scope.row.cat_level == 0" size="mini">一级</el-tag>
          <el-tag type="warning" v-else-if="scope.row.cat_level == 1" size="mini">二级</el-tag>
          <el-tag v-else size="mini">三级</el-tag>
        </template>
        <template slot="operate" slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="editCate(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteCate(scope.row)"></el-button>
        </template>
      </tree-table>
      <!-- 底部分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[1, 5, 10, 25]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
    <!-- 添加分类对话框 -->
    <AddCate ref="addCate"></AddCate>
    <EditCate ref="editCate"></EditCate>
  </div>
</template>
<script>
import AddCate from '../good/dialog/AddCate'
import EditCate from '../good/dialog/EditCate'
export default {
  components: {
    AddCate,
    EditCate
  },
  data: function () {
    return {
      // 查询条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 商品分类列表
      cateList: [],
      // 总数据条数
      total: 0,
      // 为table指定列的定义项
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          // 表示当前列定义为模板列
          type: 'template',
          // 表示当前这一列使用的模板名称
          template: 'isOk'
        },
        {
          label: '排序',
          // 表示当前列定义为模板列
          type: 'template',
          // 表示当前这一列使用的模板名称
          template: 'sort'
        },
        {
          label: '操作',
          // 表示当前列定义为模板列
          type: 'template',
          // 表示当前这一列使用的模板名称
          template: 'operate'
        }
      ]
    }
  },
  created: function () {
    this.getCateList()
  },
  methods: {
    // 获取商品分类列表
    getCateList: async function () {
      const res = await this.$http.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.warning('获取商品分类列表失败')
      }
      this.cateList = res.data.result
      this.total = res.data.total
    },
    // 监视pagesize的改变
    handleSizeChange: function (pagesize) {
      this.queryInfo.pagesize = pagesize
      this.getCateList()
    },
    // 监视当前页码的改变
    handleCurrentChange: function (newPage) {
      this.queryInfo.pagenum = newPage
      this.getCateList()
    },
    // 点击添加分类，弹出对话框
    addCate: function () {
      this.$refs.addCate.getParentCateList()
      this.$refs.addCate.dialogVisible = true
    },
    // 点击编辑，修改分类名称
    editCate: function (row) {
      this.$refs.editCate.dialogVisible = true
      this.$refs.editCate.addForm.current_name = row.cat_name
      this.$refs.editCate.addForm.id = row.cat_id
    },
    // 点击按钮，执行删除
    deleteCate: async function (row) {
      const confirmResult = await this.$confirm('删除此分类?', '提示', {
        confirmButtonText: '是',
        cancelButtonText: '否',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const res = await this.$http.delete(`categories/${row.cat_id}`)
      if (res.meta.status !== 200) {
        return this.$message.warning('删除失败')
      }
      this.$message.success('删除成功')
      this.getCateList()
    }
  }
}
</script>
<style lang="less" scoped>
.el-row {
  margin-bottom: 20px;
}
</style>
